package cn.swjtuhc.ss.hljapi.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.swjtuhc.ss.hljapi.common.Result;
import cn.swjtuhc.ss.hljapi.pojo.Activity;
import cn.swjtuhc.ss.hljapi.pojo.Essay;
import cn.swjtuhc.ss.hljapi.pojo.Viewer;
import cn.swjtuhc.ss.hljapi.pojo.request.ActivitySaveRequest;
import cn.swjtuhc.ss.hljapi.service.ActivityService;
import cn.swjtuhc.ss.hljapi.service.ViewerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 程序狗没有春天
 * @version 1.00
 * @createTime 2024/8/28 15:18
 */
@Slf4j
@RequestMapping("/api/activity/")
@RestController
public class ActivityController {
    @Autowired
    ActivityService activityService;
    @Autowired
    ViewerService viewerService;
    @GetMapping("list")
    public Result<List<Activity>> ResultfindAll() {
        return Result.success(activityService.lambdaQuery().orderByDesc(Activity::getId).eq(Activity::getVolunteerId,StpUtil.getLoginIdAsInt()).list());
    }
    @GetMapping("find")
    public Result find(){
        LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper<Activity>()
                .orderByDesc(Activity::getId)
                .last("limit 3");
        Viewer byId = viewerService.getById(1);
        byId.setViewers(byId.getViewers()+1);
        return Result.success(activityService.list(wrapper));
    }
    @PostMapping("save")
    public Result save(@RequestBody ActivitySaveRequest saverequest) {
        Activity activity = BeanUtil.copyProperties(saverequest, Activity.class);
        activity.setVolunteerId(StpUtil.getLoginIdAsInt());
//        拷贝的作用是, 将saverequest中的属性拷贝到activity中
        activityService.saveOrUpdate(activity);
        return Result.success("操作成功");
    }
    @GetMapping("detail/{id}")
    public Result ShowDetail(@PathVariable Integer id) {
        return Result.success(activityService.findById(id));
    }

    @GetMapping("delete")
    public Result delete(@RequestParam Integer id) {
        activityService.removeById(id);
        return Result.success( null);
    }
}
